import axios from 'axios'
// const isDev = true
// const publishUrl = 'https://bkycms.com';
// const fwqUrl ='/addons/yb_wm/index.php/'
// const backendPath = isDev ? publishUrl+fwqUrl : location.origin+fwqUrl;

const isDev = process.env.NODE_ENV === 'development' ? true : false
const publishUrl = 'https://bkycms.com/addons/yb_wm/index.php/'
const backendPath = isDev
  ? publishUrl
  : location.href.match(/(\S*)views/)[1] + 'index.php/'

let http = axios.create({
  baseURL: backendPath,
  withCredentials: false,
  headers: {
    // 'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8'
    'Content-Type': 'application/json'
  }
  // transformRequest: [function (data) {
  //     let newData = '';
  //     for (let k in data) {
  //         if (data.hasOwnProperty(k) === true) {
  //             newData += encodeURIComponent(k) + '=' + encodeURIComponent(data[k]) + '&';
  //         }
  //     }
  //     return newData;
  // }]
})

function apiAxios (method, url, params, response) {
  http({
    method: method,
    url: url,
    data: method === 'POST' || method === 'PUT' ? params : null,
    params: method === 'GET' || method === 'DELETE' ? params : null
  })
    .then(function (res) {
      response(res.data)
    })
    .catch(function (err) {
      response(err)
    })
}

export default {
  get: function (url, params, response) {
    return apiAxios('GET', url, params, response)
  },
  post: function (url, params, response) {
    return apiAxios('POST', url, params, response)
  },
  put: function (url, params, response) {
    return apiAxios('PUT', url, params, response)
  },
  delete: function (url, params, response) {
    return apiAxios('DELETE', url, params, response)
  },
  backendPath,
  isDev
}
